package com.hfzy.ihk.web.linkcall.handlers;

import com.alibaba.fastjson.JSON;
import com.hfzy.ihk.common.core.elasticsearch.ElasticsearchOperation;
import com.hfzy.ihk.common.core.elasticsearch.base.CommonTableOperationHandler;
import com.hfzy.ihk.facade.linkcall.entity.TestLog;
import com.hfzy.ihk.facade.linkcall.enums.LinkCallOperations;
import com.hfzy.ihk.facade.linkcall.enums.LinkCallTables;
import com.hfzy.ihk.web.linkcall.biz.LogBiz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/7/19.
 *
 * @author wws
 */
@Component
public class LinkcallTableOperationHandler extends CommonTableOperationHandler {

    private static final Logger logger = LoggerFactory.getLogger(LinkcallTableOperationHandler.class);

    @Autowired
    LogBiz logBiz;

    public LinkcallTableOperationHandler() {

        /**
         * 必须定义该处理器对应的table和操作名
         * */
        this.table = LinkCallTables.TEST_LOG;

        this.tableOperation = LinkCallOperations.addTestLog;
    }


    @Override
    protected void toHandle(ElasticsearchOperation elasticsearchOperation) {


        List data = elasticsearchOperation.getData();

        logger.debug("handle data:{}", JSON.toJSONString(data));

        if(data != null && data.size()>0) {

            List<TestLog> logs= (List<TestLog>) data.stream().map(d-> JSON.parseObject(JSON.toJSONString(d),TestLog.class)).collect(Collectors.toList());

            for(Object o:data){
                TestLog log = JSON.parseObject(JSON.toJSONString(o),TestLog.class);
                System.out.println(JSON.toJSONString(log));
            }

            logBiz.addLog(logs);
        }

    }

}
